﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;

namespace Yxinmt.Controls
{
    public class TextBlock : BaseControl, IControl
    {
        private System.Windows.Controls.TextBlock _TextBlock { get; set; }

        public TextBlock(Dictionary<string, object> property, IComponent component, bool isFactory = false)
        {
            Property = property;
            _Component = component;
            _IsFactory = isFactory;
            if (_IsFactory)
            {
                _FactoryPropertyList = new List<FactoryProperty>();
                _ControlType = typeof(System.Windows.Controls.TextBlock);
            }

            _TextBlock = new System.Windows.Controls.TextBlock();
            this.Element = _TextBlock;

            InitSet();
            SetProperty();
            SetControlProperty();
            SetTextFont();
        }

        private void SetProperty()
        {
            this.SetDefaultValue("TextWrapping", "Wrap");
        }

        private void SetTextFont()
        {
            string fontFamily = this.Property.GetStringValue("FontFamily");
            double fontSize = this.Property.GetValue<double>("FontSize");
            string fontColor = this.Property.GetStringValue("FontColor");

            if (!string.IsNullOrEmpty(fontFamily))
            {
                this._TextBlock.FontFamily = new System.Windows.Media.FontFamily(fontFamily);
                this.SetFactoryProperty("FontFamily", this._TextBlock.FontFamily);
            }
            if (fontSize > 0)
            {
                this._TextBlock.FontSize = fontSize;
                this.SetFactoryProperty("FontSize", this._TextBlock.FontSize);
            }
            if (!string.IsNullOrEmpty(fontColor))
            {
                this._TextBlock.Foreground = new System.Windows.Media.SolidColorBrush(Utility.Common.GetColor(fontColor));
                this.SetFactoryProperty("Foreground", this._TextBlock.Foreground);
            }
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.TextBlock c = _TextBlock;
            switch (name)
            {
                case "name": c.Name = string.Concat("tbk_", (string)value); SetFactoryProperty("Name", c.Name); break;
                case "Text": c.Text = (string)value; SetFactoryProperty("Text", c.Text); break;
                case "Width":
                case "ControlWidth": c.Width = Convert.ToDouble(value); SetFactoryProperty("Width", c.Width); break;
                case "Height": c.Height = Convert.ToDouble(value); SetFactoryProperty("Height", c.Height); break;
                case "BackColor": c.Background = GetColor((string)value); SetFactoryProperty("Background", c.Background); break;
                case "TextAlign":
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Padding": c.Padding = this.GetThickness((string)value); SetFactoryProperty("Padding", c.Padding); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "TextWrapping": c.TextWrapping = GetTextWrapping((string)value); SetFactoryProperty("TextWrapping", c.TextWrapping); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
                case "Value": SetValue(value); break;
            }
        }

        protected override void SetFactoryProperty(string name, object value)
        {
            if (!_IsFactory) return;

            switch (name)
            {
                case "FontFamily": this.AddFactoryProperty(System.Windows.Controls.TextBlock.FontFamilyProperty, value); break;
                case "FontSize": this.AddFactoryProperty(System.Windows.Controls.TextBlock.FontSizeProperty, value); break;
                case "Foreground": this.AddFactoryProperty(System.Windows.Controls.TextBlock.ForegroundProperty, value); break;
                case "Text": this.AddFactoryProperty(System.Windows.Controls.TextBlock.TextProperty, value); break;
                case "Margin": this.AddFactoryProperty(System.Windows.Controls.TextBlock.MarginProperty, value); break;
                case "name": this.AddFactoryProperty(System.Windows.Controls.TextBlock.NameProperty, value); break;
                case "Height": this.AddFactoryProperty(System.Windows.Controls.TextBlock.HeightProperty, value); break;
                case "Width": this.AddFactoryProperty(System.Windows.Controls.TextBlock.WidthProperty, value); break;
                case "Padding": this.AddFactoryProperty(System.Windows.Controls.TextBlock.PaddingProperty, value); break;
                case "VerticalAlignment": this.AddFactoryProperty(System.Windows.Controls.TextBlock.VerticalAlignmentProperty, value); break;
                case "HorizontalAlignment": this.AddFactoryProperty(System.Windows.Controls.TextBlock.HorizontalAlignmentProperty, value); break;
                case "Visibility": this.AddFactoryProperty(System.Windows.Controls.TextBlock.VisibilityProperty, value); break;
                case "Background": this.AddFactoryProperty(System.Windows.Controls.TextBlock.BackgroundProperty, value); break;
                case "TextWrapping": this.AddFactoryProperty(System.Windows.Controls.TextBlock.TextWrappingProperty, value); break;
            }
        }

        public override object GetValue()
        {
            return this._TextBlock.Text;
        }

        public override void SetValue(object value)
        {
            _TextBlock.Text = GetValueText(value);
        }
    }
}
